/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controle;

import java.io.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import modelo.Despesa;
import modelo.Exercicio;
import modelo.Subcategoria;
import persistencia.DespesasDAO;
import persistencia.ExercicioDAO;
import persistencia.SubcategoriaDAO;

/**
 *
 * @author aldiene
 */
public class ServletDespesas extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String acao = request.getParameter("acao");

        if (acao.equals("salvar")) {//cadastra despesas

            salvar(request, response);

        } else if (acao.equals("listar")) {//listar despesas

            listar(request, response);

        } else if (acao.equals("editarDespesas")) {
            editar(request, response);
            String descricao = request.getParameter("descricao");
            String data = request.getParameter("data");
            String valor = request.getParameter("valor");

        }else if (acao.equals("excluir")) {//listar despesas
            excluir(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }

    protected void salvar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        String descricao = request.getParameter("descricao");
        String pdata = request.getParameter("data");
        String pExec = request.getParameter("exercicio");
        String pvalor = request.getParameter("valor");
        String psubcat = request.getParameter("subcategoria");
        psubcat = psubcat.trim();
        
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");  
        java.util.Date data = null;
        try {
            data = new java.util.Date(format.parse(pdata).getTime());          
        } catch (ParseException e) {
            System.err.print(e);
        }
        float valor = Float.parseFloat(pvalor);

        Despesa despesa = new Despesa(descricao, data, valor);
       
        Subcategoria subcat = SubcategoriaDAO.getSubcategoria(Integer.parseInt(psubcat));
        despesa.setSubcategoria(subcat);
        
        Exercicio exec = ExercicioDAO.getExercicio(Integer.parseInt(pExec));
        
        despesa.setExercicio(exec);
        DespesasDAO.salvar(despesa);
        
        getServletContext().getRequestDispatcher("/CadastrarDespesas.jsp").forward(request, response);
   }

    //METODO LISTAR
    protected void listar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);


        DespesasDAO dao = new DespesasDAO();
        Vector despesas = dao.getDespesas();
        request.setAttribute("despesas", despesas);
        getServletContext().getRequestDispatcher("/ListarExcluirDespesas.jsp").forward(request, response);
    }

      protected void excluir(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        int id = Integer.parseInt(request.getParameter("id"));
        DespesasDAO dao = new DespesasDAO();
        dao.excluir(id);
        getServletContext().getRequestDispatcher("/ServletDespesas?acao=listar").forward(request, response);


    }
    
    protected void editar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        try {
            int id = Integer.parseInt(request.getParameter("id_despesas"));
            String descricao = request.getParameter("descricao");
            String pdata = request.getParameter("data");
            String pvalor = request.getParameter("valor");

            float valor = Float.parseFloat(pvalor);
            Date data = new Date(pdata);
            Despesa despesas = new Despesa(descricao, data, valor);
            despesas.setId(id);
            DespesasDAO des = new DespesasDAO();
            des.editarDespesa(despesas);

            session.setAttribute("despesas", des);
            response.sendRedirect("/AlterarDespesas.jsp");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
